<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

/**
 * 微信文本消息保存处理
 * @package App\Jobs
 */
class WxmsgTextHandle implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $appid;
    private $content;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($appid, $content)
    {
        $this->appid = $appid;
        $this->content = $content;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        return \App\Models\WxmsgText::updateOrCreate(
            [
                'appid' => $this->appid,
                'msg_id' => $this->content['MsgId'] ?? 0,
            ],
            [
                'msg_type' => $this->content['MsgType'] ?? '',
                'to_user_name' => $this->content['ToUserName'] ?? '',
                'from_user_name' => $this->content['FromUserName'] ?? '',
                'content' => $this->content['Content'] ?? '',
                'create_time' => $this->content['CreateTime'] ?? time(),
            ]
        );
    }
}
